//
//  Extension+Color.swift
//  ShiGuangJiaSwift
//
//  Created by 汪志浩 on 2022/11/15.
//

import Foundation
import UIKit

public extension UIColor {

    convenience init(r:UInt32 ,g:UInt32 , b:UInt32 , a:CGFloat = 1.0) {
        self.init(red: CGFloat(r) / 255.0,
                  green: CGFloat(g) / 255.0,
                  blue: CGFloat(b) / 255.0,
                  alpha: a)
    }
    
    convenience init?(hex: String) {
        //校验该 hex 字符串是否符合规则
        guard hex.count > 6 else { return nil }
        //hex字符串可能是以 `##` `#` `0x` `0X`开头
        var tempHex = hex.uppercased()
        
        if tempHex.hasPrefix("##") || tempHex.hasPrefix("0X") {
            tempHex = (tempHex as NSString).substring(from: 2)
        }
        
        if tempHex.hasPrefix("#") {
            tempHex = (tempHex as NSString).substring(from: 1)
        }
        
        var range = NSRange(location: 0, length: 2)
        let rHex = (tempHex as NSString).substring(with: range)
        range.location = 2
        let gHex = (tempHex as NSString).substring(with: range)
        range.location = 4
        let bHex = (tempHex as NSString).substring(with: range)
        
        var r:UInt32 = 0
        var g:UInt32 = 0
        var b:UInt32 = 0
        
        Scanner(string: rHex).scanHexInt32(&r)
        Scanner(string: gHex).scanHexInt32(&g)
        Scanner(string: bHex).scanHexInt32(&b)
        
        self.init(r: r, g: g, b: b)
    }
    
    class var random: UIColor {
        return UIColor(r: arc4random_uniform(256),
                       g: arc4random_uniform(256),
                       b: arc4random_uniform(256))
    }
    
    func image() -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(self.cgColor)
        context!.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
    
    //    func getRGBFromColor(color:UIColor) -> (r:CGFloat,g:CGFloat,b:CGFloat) {
    //        var color = UIColor.clear
    //        var numComponents = (color as! CGColor).numberOfComponents
    //        if (numComponents == 4) {
    //            let components = CGColorGetComponents(color)
    //            R = components[0];
    //            G = components[1];
    //            B = components[2];
    //        }
    //    }
    
    ///  //绘制渐2变色
    /// - Parameters:
    ///   - forView: 目标视图
    ///   - folor: 色值
    ///   - withDicrection: 方向 1左右  2上下
    static func drawGradientColor(forView:UIView,
                                  colors:[CGColor],
                                  range:[NSNumber],
                                  withDicrection:Int){
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = forView.bounds
        
        gradientLayer.colors = colors
        let gradientLocations:[NSNumber] = range
        gradientLayer.locations = gradientLocations
        if withDicrection == 1{
            gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
        }else{
            gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint.init(x: 0, y: 1)
        }
        forView.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    convenience init(wzhhex: String) {
        //校验该 hex 字符串是否符合规则
        guard wzhhex.count > 6 else {
            self.init(r: 0, g: 0, b: 0)
            return
        }
        //hex字符串可能是以 `##` `#` `0x` `0X`开头
        var tempHex = wzhhex.uppercased()
        
        if tempHex.hasPrefix("##") || tempHex.hasPrefix("0X") {
            tempHex = (tempHex as NSString).substring(from: 2)
        }
        
        if tempHex.hasPrefix("#") {
            tempHex = (tempHex as NSString).substring(from: 1)
        }
        
        var range = NSRange(location: 0, length: 2)
        let rHex = (tempHex as NSString).substring(with: range)
        range.location = 2
        let gHex = (tempHex as NSString).substring(with: range)
        range.location = 4
        let bHex = (tempHex as NSString).substring(with: range)
        
        var r:UInt32 = 0
        var g:UInt32 = 0
        var b:UInt32 = 0
        
        Scanner(string: rHex).scanHexInt32(&r)
        Scanner(string: gHex).scanHexInt32(&g)
        Scanner(string: bHex).scanHexInt32(&b)
        
        self.init(r: r, g: g, b: b)
    }
    
    convenience init(wzhhex: String,a:CGFloat = 1.0) {
        //校验该 hex 字符串是否符合规则
        guard wzhhex.count > 6 else {
            self.init(r: 0, g: 0, b: 0)
            return
        }
        //hex字符串可能是以 `##` `#` `0x` `0X`开头
        var tempHex = wzhhex.uppercased()
        
        if tempHex.hasPrefix("##") || tempHex.hasPrefix("0X") {
            tempHex = (tempHex as NSString).substring(from: 2)
        }
        
        if tempHex.hasPrefix("#") {
            tempHex = (tempHex as NSString).substring(from: 1)
        }
        
        var range = NSRange(location: 0, length: 2)
        let rHex = (tempHex as NSString).substring(with: range)
        range.location = 2
        let gHex = (tempHex as NSString).substring(with: range)
        range.location = 4
        let bHex = (tempHex as NSString).substring(with: range)
        
        var r:UInt32 = 0
        var g:UInt32 = 0
        var b:UInt32 = 0
        
        Scanner(string: rHex).scanHexInt32(&r)
        Scanner(string: gHex).scanHexInt32(&g)
        Scanner(string: bHex).scanHexInt32(&b)
        
        self.init(r: r, g: g, b: b,a:a)
    }
}
